package com.jin.webvideo.videoCache;

import com.jin.webvideo.util.cloneUtil.Cloneable;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;
@Cloneable
public class TSFile {
    private M3U8File file;
    private String url;
    private List<SegmentParam> tags;
    public TSFile(){}
    public TSFile(M3U8File file){
        this(file , null);
    }
    public TSFile(M3U8File file, String url){
        this.url = url;
        this.file = file;
        tags = new ArrayList<>();
    }
    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public List<SegmentParam> getTags() {
        return tags;
    }

    public M3U8File getFile() {
        return file;
    }
    public String getAbslotePath(){
        return resolve(url ,file.getSource());
    }

    public void setTags(List<SegmentParam> tags) {
        this.tags = tags;
    }
    public void addTag(SegmentParam param){
        tags.add(param);
    }
    public boolean hasTag(String name) {
        if (name == null || tags == null) {
            return false;
        }
        for (SegmentParam tag :tags) {
            if (name.equalsIgnoreCase(tag.getName())) {
                return true;
            }
        }
        return false;
    }
    private String resolve(String url, String baseUrl){
        if (baseUrl == null){
            return url;
        }
        URI baseUri = URI.create(baseUrl);
        URI r = baseUri.resolve(url);
        return r.toASCIIString();
    }
}
